package com.asa.ui.I;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;

import javax.swing.JFrame;
import javax.swing.JPanel;

import com.asa.ui.I.sort.SelectionSortData;
import com.asa.ui.frame.AlgoVisHelper;

public class AlgoFrame extends JFrame{
	/**
	 * 
	 */
	private static final long serialVersionUID = -3035088527551930125L;
	private int canvasWidth;//画布的宽
	private int canvasHeight;//画布的长

	private SelectionSortData data;
	
	
   public int getCanvasWidth() {
		return canvasWidth;
	}
	public int getCanvasHeight() {
		return canvasHeight;
	}
	public void render(SelectionSortData data){
		this.data = data; 
		repaint();
	}
   public AlgoFrame(String title,int  canvasWidth, int canvasHeight,SelectionSortData data){
	  super(title);
	  this.canvasWidth=canvasWidth;
	  this.canvasHeight=canvasHeight;
	  this.data = data;
	  setDefaultCloseOperation(EXIT_ON_CLOSE);
	  setResizable(false);//关闭可拖动
	  setVisible(true);
	  AlgoCanvas algoCanvas = new AlgoCanvas();
	  setContentPane(algoCanvas);
	  pack();
   }
   //设置面板JPanel
   class AlgoCanvas extends JPanel{
	/**
	 * 
	 */
	private static final long serialVersionUID = -6056196800598676936L;
    private  AlgoCanvas(){
    	//开启双缓存
    	super(true);
    }
	@Override
	//在面板上绘制内容
	protected void paintComponent(Graphics g) {
		super.paintComponent(g);
		Graphics2D g2d=(Graphics2D) g;
		AlgoVisHelper.setRenderingHints(g2d);//设置抗锯齿
		AlgoVisHelper.setStrokeWidth(g2d, 1);//设置边的宽度为5
		
		//具体绘制
		int w = canvasWidth/data.N();
		for (int i = 0; i < data.N(); i++) {
			
			if (i<data.orderedIndex) {
				AlgoVisHelper.setColor(g2d, Color.RED);
			}else {
				AlgoVisHelper.setColor(g2d, Color.GRAY);
			}
			if (i == data.currentCompareIndex) {
				AlgoVisHelper.setColor(g2d, Color.YELLOW);
			}
			if (i == data.currentMinIndex) {
				AlgoVisHelper.setColor(g2d, Color.BLUE);
			}
			
			AlgoVisHelper.fillReactangle(g2d, i*w, canvasHeight-data.get(i), w-1, data.get(i));
		
		
		}
		
		
	}
	@Override
	public Dimension getPreferredSize() {
		return new Dimension(canvasWidth,canvasHeight);
	}
   }
   
}
